﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;
using ABCTile.ViewModel;

namespace ABCTile.Controllers
{ 
    public class EstimateController : Controller
    {
        private ABCDbContext db = new ABCDbContext();
        EstimateDetail EDetail = new EstimateDetail();
        public decimal MyTotal = 0;
        //
        // GET: /Estimate/

        //public ViewResult Index()
        //{
        //    var Estimates = db.Estimates.Include(i => i.EstimateDetails);
        //    return View(db.Estimates.ToList());
        //}
        /***********************/
        public ActionResult Index(Int32? id, Int32? estimateDetailID, Int32? test )
        {
            ViewBag.Estimate = db.Estimates.Select(r => r.Id).Distinct();
            
            var viewModel = new EstimateIndexData();            
            var zquery = "SELECT Estimate.* from Estimate LEFT OUTER JOIN EstimateDetail ON Estimate.Id = EstimateDetail.Id ";

            if (id != null)
            {
                zquery += "WHERE Estimate.Id = " + id;
            }
            //if (test != null)
            //{
            //    zquery += "WHERE Estimate.EstimateTotal = " + test.Value;
            //}
            viewModel.Estimates = db.Database.SqlQuery<Estimate>(zquery);


            if (id != null)
            {
                ViewBag.EstimateID = id.Value;
                var z2query = "SELECT * FROM EstimateDetail where EstimateId = " + id;
                viewModel.EstimateDetails = db.Database.SqlQuery<EstimateDetail>(z2query);
            }


            if (id != null)
            {
                ViewBag.EstimateID = id.Value;
                var CountQuery = "SELECT COUNT(*) AS Count FROM EstimateDetail ";
                if (id != null)
                {
                    CountQuery += "WHERE EstimateId = " + id;
                }
                //System.Convert.ChangeType(source, destinationType);
                //EstimateTotal CountTest = db.Database.SqlQuery<EstimateTotal>(CountQuery).Single();
                EstimateCount CountTest = db.Database.SqlQuery<EstimateCount>(CountQuery).Single();
                if (CountTest.Count > 0)
                {
                    var TotalQuery = "SELECT SUM(Quantity * PricePerQty) AS Total FROM EstimateDetail ";

                    if (id != null)
                    {
                        TotalQuery += "WHERE EstimateId = " + id;
                    }
                    EstimateTotal zouTest = db.Database.SqlQuery<EstimateTotal>(TotalQuery).Single();
                    MyTotal = zouTest.Total;
                    ViewBag.zoutotal = zouTest.Total;
                }
                Estimate estimate = db.Estimates.Find(id);
                estimate.EstimateTotal = MyTotal;
                db.Entry(estimate).State = EntityState.Modified;
                db.SaveChanges();
            }

            return View(viewModel);
        }

        //public ActionResult GetTotal (Int32? id, Int32? estimateDetailID)
        //{
        //    decimal? total = 0;

        //    return total ?? decimal.Zero;
        //}

        //public decimal GetTotal()
        //{
        //    decimal? total =
        //        (from EstimateDetail in ABCTile.Models
        //         where cartItems.CartId == eventCartId
        //         select (int?)cartItems.Count * cartItems.Ticket.Price)
        //        .Sum();

        //    return total ?? decimal.Zero;
        //}
        /***********************/

        public ViewResult Details(int id)
        {
            Estimate estimate = db.Estimates.Find(id);
            return View(estimate);
        }

        //
        // GET: /Estimate/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Estimate/Create

        [HttpPost]
        public ActionResult Create(Estimate estimate)
        {
            if (ModelState.IsValid)
            {
                db.Estimates.Add(estimate);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(estimate);
        }

        
        //
        // GET: /Estimate/Edit/5
 
        public ActionResult Edit(int id)
        {
            Estimate estimate = db.Estimates.Find(id);
            return View(estimate);
        }

        //
        // POST: /Estimate/Edit/5

        [HttpPost]
        public ActionResult Edit(Estimate estimate)
        {
            if (ModelState.IsValid)
            {
                db.Entry(estimate).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(estimate);
        }

        //
        // GET: /Estimate/Delete/5
 
        public ActionResult Delete(int id)
        {
            Estimate estimate = db.Estimates.Find(id);
            return View(estimate);
        }

        //
        // POST: /Estimate/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Estimate estimate = db.Estimates.Find(id);
            db.Estimates.Remove(estimate);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


    /************************/
        //
        // GET: /EstimateDetail/Create

        public ActionResult EDCreate()
        {
            return View();
        }

        //
        // POST: /EstimateDetail/Create

        [HttpPost]
        public ActionResult EDCreate(EstimateDetail estimatedetail)
        {
            if (ModelState.IsValid)
            {
                db.EstimateDetails.Add(estimatedetail);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(estimatedetail);
        }

        //
        // GET: /EstimateDetail/Edit/5
 
        public ActionResult EDEdit(int id)
        {
            //Estimate estimate = db.Estimates.Find(id);
            //return View(estimate);
            EstimateDetail estimatedetail = db.EstimateDetails.Find(id);
            return View(estimatedetail);
        }

        //
        // POST: /Estimate/Edit/5

        [HttpPost]
        public ActionResult EDEdit(EstimateDetail estimatedetail)
        {
            if (ModelState.IsValid)
            {
                db.Entry(estimatedetail).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(estimatedetail);
        }

        //
        // GET: /EstimateDetail/Details/5

        public ViewResult EDDetails(int id)
        {
            EstimateDetail estimatedetail = db.EstimateDetails.Find(id);
            return View(estimatedetail);
        }

        public ActionResult EDDelete(int id)
        {
            EstimateDetail estimatedetail = db.EstimateDetails.Find(id);
            return View(estimatedetail);
        }

        //
        // POST: /EstimateDetail/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult EDDeleteConfirmed(int id)
        //{
        //    EstimateDetail estimatedetail = db.EstimateDetails.Find(id);
        //    db.EstimateDetails.Remove(estimatedetail);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}


    /************************/
    }
}